package com.shaco.heroproject.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.shaco.heroproject.R;
import com.shaco.heroproject.entity.Result_ability;
import com.shaco.heroproject.volley.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 15/11/5.
 */
public class AbilityAdapter extends RecyclerView.Adapter<AbilityAdapter.AbilityHolder> {
    private List<Result_ability> result;
    private VolleySingleton queue;


    public AbilityAdapter(Context c) {
        //queue = Volley.newRequestQueue(c);
    }

    @Override
    public AbilityHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        View v = inflater.inflate(R.layout.activity_ability_item, null);
        return new AbilityHolder(v);
    }

    @Override
    public void onBindViewHolder(AbilityHolder holder, int position) {
        // TODO Auto-generated method stub
        if (result != null && result.size() > 0) {
            Result_ability results = result.get(position);
            holder.abilityListNameTv.setText(results.getName());
            holder.abilityLevelTv.setText("等级" + results.getTier());
            holder.abilityListTagTv.setText(results.getDescription());
            holder.abilityPriceTv.setText("价格:" + results.getPrice());
            if (results.getIcon() != null
                    && results.getIcon().length() > 0) {
                String imgUrl = results.getIcon();
                getBitmap(imgUrl, holder.abilityListImg);
            }

        }
    }

    // 解析图片
    public void getBitmap(String url, final ImageView abilityListImg) {

        ImageLoader.ImageListener listener = ImageLoader.getImageListener(abilityListImg, R.drawable.ic_launcher, R.drawable.ic_launcher);
        VolleySingleton.getVolleyIntance().getImageLoader().get(url, listener);

//        //////////////////////////
//        queue = VolleySingleton.getVolleyIntance();
//        // TODO Auto-generated method stub
//        ImageRequest request = new ImageRequest(url,
//                new Response.Listener<Bitmap>() {
//
//                    @Override
//                    public void onResponse(Bitmap response) {
//                        // TODO Auto-generated method stub
//                        // 返回正确图片的回调方法
//                        abilityListImg.setImageBitmap(response);
//                    }
//
//                }, 800, 500, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
//
//            @Override
//            public void onErrorResponse(VolleyError error) {
//                // TODO Auto-generated method stub
//                error.printStackTrace();
//            }
//        }) {
//
//            @Override
//            public Map<String, String> getHeaders() throws AuthFailureError {
//                Map<String, String> map = new HashMap<String, String>();
//                // 在请求头中添加apikey
//                map.put("Accept-Encoding", "");
//                return map;
//            }
//
//        };
//        //queue.add(request);
//        queue.addRequest(request);

        VolleySingleton.getVolleyIntance().getImage(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                abilityListImg.setImageBitmap(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

    }


    @Override
    public int getItemCount() {
        return result != null && result.size() > 0 ? result.size() : 0;
    }


    // 增加数据
    public void addData(List<Result_ability> results) {
        result = results;
        notifyDataSetChanged();
    }

    class AbilityHolder extends RecyclerView.ViewHolder {
        private ImageView abilityListImg;
        private TextView abilityListNameTv, abilityLevelTv, abilityListTagTv, abilityPriceTv;

        public AbilityHolder(View itemView) {
            super(itemView);
            abilityListImg = (ImageView) itemView.findViewById(R.id.abilityListImg);
            abilityListNameTv = (TextView) itemView.findViewById(R.id.abilityListNameTv);
            abilityLevelTv = (TextView) itemView.findViewById(R.id.abilityLevelTv);
            abilityListTagTv = (TextView) itemView.findViewById(R.id.abilityListTagTv);
            abilityPriceTv = (TextView) itemView.findViewById(R.id.abilityPriceTv);

        }
    }
}
